<template>
  <div class="search_list">
    <ul class="friends_list">
      <div class="list_title">好友</div>
      <li class="friend_msg" v-for="(item,index) in list" :key="index" @click="transMessage(item)">
        <img class="friend_avatar" :src="item.friendHeadImg" />
        <div class="friend_name">
          <span>{{item.friendRemarks}}</span>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import bus from '../../bus'
import {chatList} from '../../api/api'
export default {
  name:'search_list',
    data(){
        return {
          list:''
        }
    },
    mounted(){
      bus.$on('searchFriendList',list => {
        this.list = list
      })
    },
    methods:{
      //点击发送消息按钮
      transMessage(item){
        let params = {
            sendUserId:this.$store.state.user.id,
            toId:item.friendId,
            globalId:0,
            pageSize:100
        }
        chatList(params)
        .then(res => {
          if(res.data&&res.data.data.list){
            var newChat = res.data.data.list[0]
            if(newChat.sendUserId!=this.$store.state.user.id){
              var toId = newChat.toId
              var toUserImg = newChat.toUserImg
              var toUserName = newChat.toUserName

              newChat.toId = item.friendId
              newChat.toUserImg = item.friendHeadImg
              newChat.toUserName = item.friendRemarks

              newChat.sendUserId = toId
              newChat.sendUserImg = toUserImg
              newChat.sendUserName = toUserName
            }else{
              newChat.toId = item.friendId
              newChat.toUserImg = item.friendHeadImg
              newChat.toUserName = item.friendRemarks
            }
          }else{
            var newChat = {
              content: "",
              contentImg: "",
              contentVideo: "",
              globalId: "",
              id: 0,
              index: 0,
              messageId: 1,
              messageType: 1,
              mine: 0,
              readStatus: 0,
              sendTime: "",
              sendUserId: this.$store.state.user.id,
              sendUserImg: this.$store.state.user.headImg,
              sendUserName: this.$store.state.user.userName,
              serviceTime: "",
              toId: item.friendId,
              toUserImg: item.friendHeadImg,
              toUserName: item.friendRemarks
            }
          }
          newChat.type = 1
          this.$store.commit('setChatList',newChat)
          this.$store.commit('chooseTalk',0)
          this.$store.commit('setNav','talk')
          this.$emit("changeSearchState", {choose:1,state:false})
        })
      },
    }
}
</script>

<style scoped>
.list_title{
  line-height: 27px;
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
}
.friend_msg{
  display: flex;
  padding: 10px;
  height: 60px;
  cursor: pointer;
  display: flex;
  align-items: center;
  box-sizing: border-box;
}
.friend_msg:hover{
  background: #fbfbfb;
}
.friend_avatar{
  width:34px;
  height:34px;
}
.friend_name{
  padding:0 10px;
  font-size: 14px;
  color: #333;
}
</style>